package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/*
1.web.xml位置错误
2.static文件夹位置错误
3.mapper文件夹的位置错误
4.没有install 直接 jetty web项目
5.install选错了
6.密码的问题

 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    @Reference
    private RoleService roleService;

    @Reference
    private PermissionService permissionService;

    @RequestMapping
    public String index(HttpServletRequest request,Model model){
        //获取查询条件roleName,roleCode 和分页参数 pageNum,pageSize
        Map<String,Object> filters = getFilters(request);
        //访问业务层查询指定数据（把查询条件和分页参数传输到业务层）
        PageInfo<Role> page  = roleService.findPage(filters);
        //保存数据到request域，页面跳转
        model.addAttribute("page",page); //key是什么，要和页面一致，有分页的所有信息：数据、pageNum、pageSize,total,prevPage,nextPage....
        model.addAttribute("filters",filters); //这里面只有分页的两个参数： pageNum,pageSize
        return "role/index";
    }



    @RequestMapping("/create")
    public String create(){
        return "role/create";
    }

    @RequestMapping("/save")
    public String save(Role role, Model model){
        Integer n = this.roleService.insert(role);
        //return "role/create";
        //return "role"; // /templates/role.html
        //return "redirect:/role";
        model.addAttribute("messagePage","添加角色成功");
        return "common/successPage";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Integer id, Model model){
        Role role = this.roleService.getById(id);
        //model.addAttribute("role","role");
        model.addAttribute("role",role);
        return "role/edit";
    }

    @RequestMapping("/update")
    public String update(Role role, Model model){
        Integer n = this.roleService.update(role);
        model.addAttribute("messagePage","修改角色成功");
        return "common/successPage";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Integer id){
        this.roleService.delete(id);
        return "redirect:/role";
    }

    @RequestMapping("/assignShow/{id}")
    public String assignShow(@PathVariable Long id,Model model ){
        List zNodes = this.permissionService.findPermissionsByRoleId(id);
        model.addAttribute("roleId",id);
        model.addAttribute("zNodes",zNodes);
        return "role/assignShow";
    }
    @RequestMapping("/assignPermission")
    public  String assignPermission(Long roleId,Long [] permissionIds){

        this.permissionService.saveRolePermissionRealtionShip(roleId,permissionIds);

        return "common/successPage";
    }
}
